package com.abel.roommanager.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;

import com.abel.roommanager.pojo.Student;
import com.abel.roommanager.provider.StudentsCountSql;
import com.abel.roommanager.provider.StudentsSql;

public interface StudentMapper {

    @Select("select * from student limit #{index},#{count}")
    @Results({
    		@Result(id=true,column="id",property="id"),
    		@Result(column="sname",property="sname"),
    		@Result(column="sex",property="sex"),
    		@Result(column="tel",property="tel"),
    		@Result(column="headimage",property="headimage"),
    		@Result(column="livetime",property="livetime"),
    		@Result(column="cid",property="clazz",one=@One(
    				select="com.abel.roommanager.mapper.ClazzMapper.findClazzByCid"
    				)),
    		@Result(column="roomno",property="room",one=@One(
    				select="com.abel.roommanager.mapper.RoomMapper.findRoomById"
    				))
    })
    
	public List<Student> findStudentsByCurPage(@Param("index")int index, @Param("count")int count);
    @Select("select count(*) from student")
	public int findCountStudents();
	
	@Select("select count(*) from student where cid=#{cid}")
    int findStudentsCountByCid(int cid);
	@Select("select roomno from student where id=#{id}")
	public int findRoomnoById(int id);
	@Delete("delete from student where id=#{id}")
	public int delStuById(int id);
	@Insert("insert into student (sname,sex,roomno,tel,cid,headimage,livetime) values(#{sname},#{sex},#{roomno},#{tel},#{cid},#{headimage},#{livetime})")
	public int addStudent(Student stu);
	@Select("select * from student where id=#{id}")
	@Results({
		@Result(id=true,column="id",property="id"),
		@Result(column="sname",property="sname"),
		@Result(column="sex",property="sex"),
		@Result(column="tel",property="tel"),
		@Result(column="headimage",property="headimage"),
		@Result(column="livetime",property="livetime"),
		@Result(column="cid",property="clazz",one=@One(
				select="com.abel.roommanager.mapper.ClazzMapper.findClazzByCid"
				)),
		@Result(column="roomno",property="room",one=@One(
				select="com.abel.roommanager.mapper.RoomMapper.findRoomById"
				))
	})
	public Student findStudentById(int id);
	   @Update("update student set roomno=#{roomno} where id=#{id}")
	public int updateRoomnoBySid(Student stu);
	   @SelectProvider(type=StudentsSql.class,method="getFindStudentSql")
	   @Results({
			@Result(id=true,column="id",property="id"),
    		@Result(column="sname",property="sname"),
    		@Result(column="sex",property="sex"),
    		@Result(column="tel",property="tel"),
    		@Result(column="headimage",property="headimage"),
    		@Result(column="livetime",property="livetime"),
    		@Result(column="cid",property="clazz",one=@One(
    				select="com.abel.roommanager.mapper.ClazzMapper.findClazzByCid"
    				)),
    		@Result(column="roomno",property="room",one=@One(
    				select="com.abel.roommanager.mapper.RoomMapper.findRoomById"
    				))
	   })
	public List<Student> findStudentsByLimitsCurPage(@Param("student")Student student, @Param("index")int index, @Param("count")int count);
	@SelectProvider(type=StudentsCountSql .class,method="getStudentCountsSql")
	public int findCountStudentsByLimits(Student student);
	@Select("select * from student where roomno=#{rid}")
	@Results({
		@Result(id=true,column="id",property="id"),
		@Result(column="sname",property="sname"),
		@Result(column="tel",property="tel"),
		@Result(column="cid",property="clazz",one=@One(
				select="com.abel.roommanager.mapper.ClazzMapper.findClazzByCid"
				))
	})
	public List<Student> findStudentsByRid(int rid);
}
